package com.semidata.trp.business.report;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.semidata.rtd.core.log.Logger;

public class Summary {

	private Map<Integer, Map<Integer, Double>> organizationCosts = new HashMap<>();
	private Map<Integer, Map<Integer, Double>> strategyCosts = new HashMap<>();
	private Map<Integer, Map<Integer, Long>> strategyImpressions = new HashMap<>();
	private Map<Integer, Long> strategyClicks = new HashMap<>();
	private Map<Integer, Long> resourceImpressions = new HashMap<>();
	private Map<Integer, Long> resourceClicks = new HashMap<>();

	public Summary(Set<MonitorReportData> set) {
		for (MonitorReportData data : set) {

			for (Entry<Integer, Map<Integer, Double>> entry : data
					.getOrganizationCosts().entrySet()) {
				Map<Integer, Double> value = organizationCosts.get(entry
						.getKey());
				if (value == null) {
					value = new HashMap<Integer, Double>();
				}
				// Logger.log("DEBUG","orgId :" + entry.getKey() + " size:" +
				// String.valueOf(value.size()));
				for (Map.Entry<Integer, Double> entryCost : entry.getValue()
						.entrySet()) {
					// Logger.log("DEBUG", "mediaType:" +
					// entryCost.getKey().toString());
					if (value.get(entryCost.getKey()) == null)
						value.put(entryCost.getKey(), entryCost.getValue());
					value.put(
							entry.getKey(),
							entryCost.getValue()
									+ value.get(entryCost.getKey()));
				}
				organizationCosts.put(entry.getKey(), value);
				/*
				 * if (value == null) value = 0.0;
				 * organizationCosts.put(entry.getKey(), entry.getValue() +
				 * value);
				 */
			}

			for (Entry<Integer, Map<Integer, Double>> entry : data
					.getStrategyCosts().entrySet()) {
				Map<Integer, Double> value = strategyCosts.get(entry.getKey());
				if (value == null) {
					value = new HashMap<Integer, Double>();
				}
				for (Map.Entry<Integer, Double> entryCost : entry.getValue()
						.entrySet()) {
					if (value.get(entryCost.getKey()) == null)
						value.put(entryCost.getKey(), entryCost.getValue());
					value.put(
							entry.getKey(),
							entryCost.getValue()
									+ value.get(entryCost.getKey()));
				}
				strategyCosts.put(entry.getKey(), value);
				/*
				 * Double value = strategyCosts.get(entry.getKey()); if (value
				 * == null) value = 0.0; strategyCosts.put(entry.getKey(),
				 * entry.getValue() + value);
				 */
			}

			for (Entry<Integer, Map<Integer, Long>> entry : data
					.getStrategyImpressions().entrySet()) {
				Map<Integer, Long> value = strategyImpressions.get(entry
						.getKey());
				if (value == null) {
					value = new HashMap<Integer, Long>();
				}
				for (Map.Entry<Integer, Long> entryCost : entry.getValue()
						.entrySet()) {
					if (value.get(entryCost.getKey()) == null)
						value.put(entryCost.getKey(), entryCost.getValue());
					value.put(
							entry.getKey(),
							entryCost.getValue()
									+ value.get(entryCost.getKey()));
				}
				strategyImpressions.put(entry.getKey(), value);
				/*
				 * Long value = strategyImpressions.get(entry.getKey()); if
				 * (value == null) value = 0L;
				 * strategyImpressions.put(entry.getKey(), entry.getValue() +
				 * value);
				 */
			}

			for (Entry<Integer, Long> entry : data.getStrategyClicks()
					.entrySet()) {
				Long value = strategyClicks.get(entry.getKey());
				if (value == null)
					value = 0L;
				strategyClicks.put(entry.getKey(), entry.getValue() + value);
			}

			for (Entry<Integer, Long> entry : data.getResourceImpressions()
					.entrySet()) {
				Long value = resourceImpressions.get(entry.getKey());
				if (value == null)
					value = 0L;
				resourceImpressions.put(entry.getKey(), entry.getValue()
						+ value);
			}

			for (Entry<Integer, Long> entry : data.getResourceClicks()
					.entrySet()) {
				Long value = resourceClicks.get(entry.getKey());
				if (value == null)
					value = 0L;
				resourceClicks.put(entry.getKey(), entry.getValue() + value);
			}
		}
	}

	public Map<Integer, Map<Integer, Double>> getOrganizationCosts() {
		return organizationCosts;
	}

	public Map<Integer, Map<Integer, Double>> getStrategyCosts() {
		return strategyCosts;
	}

	public Map<Integer, Map<Integer, Long>> getStrategyImpressions() {
		return strategyImpressions;
	}

	public Map<Integer, Long> getStrategyClicks() {
		return strategyClicks;
	}

	public Map<Integer, Long> getResourceImpressions() {
		return resourceImpressions;
	}

	public Map<Integer, Long> getResourceClicks() {
		return resourceClicks;
	}

}
